package com.titar.kslikeview.widigets;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.graphics.PointF;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.RelativeLayout;

public class KsPathAnimator extends BasePathAnimator {
    /**
     * 进入，曲线动画时长
     */
    private int mEnterDuration, mCurveDuration;

    private int mPicWidth, mPicHeight;

    private int mViewWidth, mViewHeight;

    /**
     * 已经生成的路径数目
     */
    private int mCurrentPathCounts;

    private SparseArray<CurveEvaluator> mPathArray;

    KsPathAnimator(int enterDuration, int curveDuration) {
        this.mPathArray = new SparseArray<>();
        this.mEnterDuration = enterDuration;
        this.mCurveDuration = curveDuration;
    }

    void setPic(int picWidth, int picHeight) {
        this.mPicWidth = picWidth;
        this.mPicHeight = picHeight;
    }

    void setView(int viewWidth, int viewHeight) {
        this.mViewWidth = viewWidth;
        this.mViewHeight = viewHeight;
    }

    @Override
    public void start(View child, ViewGroup parent, RelativeLayout.LayoutParams layoutParams) {
        parent.addView(child, layoutParams);
        CurveEvaluator evaluator;


        //如果已经生成的路径数目超过最大的设定，就从路径缓存中随机获取
        if (mCurrentPathCounts > MAX_PATH_COUNTS) {
            evaluator = mPathArray.get(Math.abs(mRandom.nextInt() % MAX_PATH_COUNTS) + 1);
        } else {
            //记录生成的路劲
            ++mCurrentPathCounts;

            evaluator = createPath(getTogglePoint(1), getTogglePoint(2));
            mPathArray.put(mCurrentPathCounts, evaluator);
        }

        //设置进入动画
        AnimatorSet enterAnimator = generateEnterAnimation(child);

        //设置路径动画
        ValueAnimator curveAnimator = generateCurveAnimation(evaluator, child);

        //执行动画集合
        AnimatorSet animatorSet = new AnimatorSet();

        animatorSet.playTogether(enterAnimator, curveAnimator);
        animatorSet.addListener(new AnimationEndListener(child, parent));
        animatorSet.start();
    }

    /**
     * 进入动画，动画集合
     *
     * @return
     */
    private AnimatorSet generateEnterAnimation(View target) {
        AnimatorSet enterAnimation = new AnimatorSet();
        ObjectAnimator alpha = ObjectAnimator.ofFloat(target, View.ALPHA, 0.2f, 1f);
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(target, View.SCALE_X, 0.2f, 1f);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(target, View.SCALE_Y, 0.2f, 1f);

        //加一些动画插值器
        enterAnimation.setInterpolator(new LinearInterpolator());
        enterAnimation.playTogether(alpha, scaleX, scaleY);
        enterAnimation.setDuration(mEnterDuration);
        return enterAnimation;
    }

    /**
     * 贝塞尔曲线动画
     * @param evaluator
     * @param target
     * @return 动画集合
     */
    private ValueAnimator generateCurveAnimation(CurveEvaluator evaluator, View target){
        PointF pointF1 = new PointF((float) (mViewWidth-mPicWidth)/2,(float)(mViewHeight-mPicHeight));
        float endX = (float) ((mViewWidth-mPicHeight)/2)+((mRandom.nextBoolean() ? 1 : -1) * mRandom.nextInt(100));

        PointF pointF2 = new PointF(endX,0);
        ValueAnimator curveAnimator = ValueAnimator.ofObject(evaluator,pointF1,pointF2);

        curveAnimator.addUpdateListener(new CurveUpdateLister(target));
        curveAnimator.setInterpolator(new LinearInterpolator());
        curveAnimator.setDuration(mCurveDuration);

        return curveAnimator;
    }

    private PointF getTogglePoint(int scale){
        PointF pointF = new PointF();

        pointF.x = mRandom.nextInt(mViewWidth-100);
        pointF.y = (float) mRandom.nextInt(mViewHeight-100)/scale;

        return pointF;
    }

    private class CurveUpdateLister implements ValueAnimator.AnimatorUpdateListener{
        private View mTarget;
        CurveUpdateLister(View tagert){
            this.mTarget = tagert;
        }
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            // 获取当前动画运行的状态值, 使得动画作用对象沿着曲线(涉及贝塞儿曲线)运动
            PointF value = (PointF) animation.getAnimatedValue();
            mTarget.setX(value.x);
            mTarget.setY(value.y);
            // 改变对象的透明度
            mTarget.setAlpha(1 - animation.getAnimatedFraction());
        }
    }

    private class AnimationEndListener extends AnimatorListenerAdapter{
        private View mTarget;

        private ViewGroup mParent;

        AnimationEndListener(View target, ViewGroup parent) {

            this.mTarget = target;

            this.mParent = parent;
        }

        @Override
        public void onAnimationEnd(Animator animation) {
            super.onAnimationEnd(animation);

            mParent.removeView(mTarget);
        }
    }
}
